package dk.marten.ibdrecord;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;




public class TilstandHistoryFragment extends Fragment  {

	private DBAccess datasource;
	private HistoryListAdaptor<TilstandData> adapter;
	private ListView listView;
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState)  {
		return inflater.inflate(R.layout.fragment_history, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		listView = (ListView) getView().findViewById(R.id.historyList);
		
		datasource = new DBAccess(getView().getContext());
//	    datasource.open();
	    
		List<TilstandData> values = datasource.getAllTilstandData();

	    adapter = new HistoryListAdaptor<TilstandData>(getActivity(), R.layout.history_list_item, values);
	    
	    listView.setAdapter(adapter); 
	    
	    final Activity that =  getActivity();
	    
	    listView.setOnItemClickListener(new OnItemClickListener() {
	    	  @Override
	    	  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	    		TilstandData t = adapter.getItem(position);

		   		Intent intent = new Intent(that, RedigerTilstand.class);
		   		intent.putExtra("id",t.getId()); 
		   		startActivity(intent);
	    	  }
	    	}); 

	    datasource.close();
	
	}



}
